const state = {
  isLogin: localStorage.getItem("isLogin"),
  token: localStorage.getItem("token"),
  username: localStorage.getItem("username"),
};
const mutations = {
  login(state, [token, username]) {
    state.isLogin = true;
    state.token = token;
    state.username = username;

    localStorage.setItem("isLogin", true);
    localStorage.setItem("token", token);
    localStorage.setItem("username", username);
  },
  logout(state) {
    state.isLogin = false;
    state.token = "";
    state.username = "";

    localStorage.removeItem("isLogin");
    localStorage.removeItem("token");
    localStorage.removeItem("username");
  },
};
const actions = {
  // action中可以异步操作，如发起登录http请求。action会调用mutations
  login({ commit }, user) {
    console.info("login action, username=: ", user.username);
    commit("login", ["TEST_TOKEN", user.username]);
  },
  logout({ commit, state }) {
    console.info("logout action, token=: " + state.token);
    commit("logout");
  },
};
const getters = {
  isLoggedIn: (state) => state.isLogin,
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
};
